package tgc.rj.garbage.utlis;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import tgc.rj.garbage.entity.AisleEntity;

public class AisleUtils {

    public static List<List<AisleEntity>> gd = new ArrayList<>();

    /**
     * 初始化构建数据模型
     */
    public static void init() {
        for (int i = 0; i < 60; i++) {
            List<AisleEntity> num = LitePal.where("num=?", (i + 1) + "").order("id asc").find(AisleEntity.class);
            gd.add(num);
        }
    }

    /**
     * 将数据模型持久化
     */
    public static void save() {
        for (List<AisleEntity> ab : gd) {
            for (AisleEntity as : ab) {
                as.save();
            }
        }
    }


    /**
     * 上货操作
     *
     * @param code 二维码
     * @return -1 操作失败，其他值 货道号
     */
    public static synchronized Integer push(String code) {
        int i = 0;
        int j = -1;
        if (code.startsWith("A")) {
            for (; i < 30; i++) {
                List<AisleEntity> aisleList = gd.get(i);
                if (aisleList.size() < 7) {
                    AisleEntity model = new AisleEntity();
                    model.setType(1);
                    model.setCode(code);
                    model.setNum(i + 1);
                    model.save();
                    aisleList.add(model);
                    break;
                }
            }
            if (i == 30) {
                j = -1;
            } else {
                j = i + 1;
            }

        } else {
            for (i = 30; i < 60; i++) {
                List<AisleEntity> aisleList = gd.get(i);
                if (aisleList.size() < 7) {
                    AisleEntity model = new AisleEntity();
                    model.setType(2);
                    model.setCode(code);
                    model.setNum(i + 1);
                    model.save();
                    aisleList.add(model);
                    break;
                }
            }
            if (i == 60) {
                j = -1;
            } else {
                j = i + 1;
            }
        }
        return j;
    }


    public static synchronized AisleEntity getFirst(Integer type){
        int i = 0;
        AisleEntity model = null;
        if (type == 1) {
            for (; i < 30; i++) {
                List<AisleEntity> entityList = gd.get(i);
                if (entityList.size() > 0) {
                    AisleEntity entity = entityList.get(0);
                    model = entity;

                    break;
                }

            }
        } else {
            for (i = 30; i < 60; i++) {
                List<AisleEntity> entityList = gd.get(i);
                if (entityList.size() > 0) {
                    AisleEntity entity = entityList.get(0);
                    model = entity;

                    break;
                }

            }
        }
        return model;
    }
    /**
     * 出货操作
     *
     * @param type 1:厨余 2:其它
     * @return 返回垃圾袋对象
     */
    public static synchronized AisleEntity pop(Integer type) {
        int i = 0;
        AisleEntity model = null;
        if (type == 1) {
            for (; i < 30; i++) {
                List<AisleEntity> entityList = gd.get(i);
                if (entityList.size() > 0) {
                    AisleEntity entity = entityList.get(0);
                    model = entity;
                    entityList.remove(entity);
                    entity.delete();
                    break;
                }
            }
        } else {
            for (i = 30; i < 60; i++) {
                List<AisleEntity> entityList = gd.get(i);
                if (entityList.size() > 0) {
                    AisleEntity entity = entityList.get(0);
                    model = entity;
                    entityList.remove(entity);
                    entity.delete();
                    break;
                }

            }
        }
        return model;
    }

    /***
     * 清空轨道货物
     * @param shouNum   起始轨道
     * @param weiNum    结束轨道
     */
    public static void deletetrack(Integer shouNum,Integer weiNum){
        for (int i = shouNum ; i <= weiNum ; i++){
            List<AisleEntity> list = gd.get(i - 1);
            for(AisleEntity model : list){
                model.delete();
            }
            list.clear();
        }
    }

    /**
     * 获取某种类型垃圾袋的数量
     *
     * @param type 1:厨余 2:其它
     * @return 数量
     */
    public static Integer getNum(Integer type) {
        int num = 0;
        if (type == 1) {
            for (int i = 0; i < 30; i++) {
                num += gd.get(i).size();
            }
        } else if (type == 2) {
            for (int i = 30; i < 60; i++) {
                num += gd.get(i).size();
            }
        }
        return num;
    }

    /***
     * 查询当前种类垃圾袋的剩余库存
     * @param type  类型
     * @return  库存量
     */
    public static Integer size(Integer type){
        List<AisleEntity> list = LitePal.where("type=?",type.toString()).find(AisleEntity.class);
        Integer listSize = list.size();
        return listSize;
    }
}
